package com.huangzhao.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GulimallElasticSearchConfig {

    public static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer" + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(new HttpAsyncResponseConsumerFactory
//                .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Value("${elasticsearch.ip}")
    private String elasticsearchIp;
    @Value("${elasticsearch.port}")
    private Integer elasticsearchPort;

    /**
     * 创建esClient
     *
     * @return
     */
    @Bean("restHighLevelClient")
    public RestHighLevelClient esRestClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(elasticsearchIp, elasticsearchPort, "http"));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}
